﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using EIS.DatabaseHelper;
using EIS.Obj;

namespace EIS.Data
{
    public class MarkRecordReadUnread
    {
        public static int MarkReadUnread(string functionID, DataTable dt)
        {
/*
            tableName, string[] requestedFields,
                                           KeyValuePair<string, string>[] parameters,
                                           KeyValuePair<string, string>[] operators)
*/
            DataTable dtToUpdate = dt.Clone();
            foreach (DataRow dr in dt.Rows)
            {
                var parameters = new KeyValuePair<string, string>[2];
                var operators = new KeyValuePair<string, string>[2];
                var para = new KeyValuePair<string, string> (EISConstant.EISJavaServerMainID, dr[EISConstant.EISJavaServerMainID].ToString());
                var oper = new KeyValuePair<string, string> (EISConstant.EISJavaServerMainID, "=");
                parameters[0] = para;
                operators[0] = oper;

                para = new KeyValuePair<string, string>(EISConstant.EISTimeStamp, dr[EISConstant.EISTimeStamp].ToString());
                oper = new KeyValuePair<string, string>(EISConstant.EISTimeStamp, ">");
                parameters[1] = para;
                operators[1] = oper;

                var record = new DBHandler().SelectSimple(functionID, null, parameters, operators);
                if (record.Tables[0].Rows.Count == 0)
                {
                    var drToUpdate = dtToUpdate.NewRow();
                    drToUpdate.ItemArray = dr.ItemArray;
                    dtToUpdate.Rows.Add(drToUpdate);
                }
            }

            var unreadNo = new DBHandler().UpdateRecordsReturnUnreadNumber(functionID, dtToUpdate);
            return unreadNo;
        }
        public static int MarkAllAsRead(string functionID)
        {
            var unreadNo = new DBHandler().UpdateMarkAllAsRead(functionID);
            return unreadNo;
        }


    }
}
